//
//  GoodsStandardsTableViewController.m
//  帮惠Mall
//
//  Created by reew on 2017/9/22.
//  Copyright © 2017年 reew. All rights reserved.
//

#import "GoodsStandardsTableViewController.h"
#import "SexTableViewCell.h"
#import <MJExtension.h>
#import "MycolorModel.h"
#import "ColorTableViewCell.h"
#import "ColorCollectionViewCell.h"
#import "System.h"
#import "UIColor+Hex.h"
#import "CommonUtils.h"
#import <AFNetworking.h>
#import "Admin.h"
#import <SVProgressHUD.h>
#import "SizeTableViewCell.h"
#import "AddGoodsTableViewController.h"
#import "Account.h"

@interface GoodsStandardsTableViewController ()<UITableViewDelegate,UITableViewDataSource,SizeTableViewCellDelegate,ColorTableViewCellDelegate,SexTableViewCellDelegate>
@property (nonatomic,strong) NSArray *titleArr;

@property (nonatomic,strong) NSMutableArray *mycolorModelArr;
@property (nonatomic,strong) NSArray *selectedColorArray;

@property (nonatomic,strong) NSArray *sizeArr;
@property (nonatomic,strong) NSArray *selectSizeArr;

@property (nonatomic) BOOL selectMale;
@property (nonatomic) BOOL selectFemale;



@end

@implementation GoodsStandardsTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置tableView
    [self setUpTableView];
    //解析颜色的json数据
    [self unparsedColor];
    //请求尺寸数据
    [self requestSize];
    //设置导航栏
    [self setupNav];
    
    //添加手势，点击tableview 隐藏键盘
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyBoard)];
    [self.tableView addGestureRecognizer:tapGestureRecognizer];
    tapGestureRecognizer.cancelsTouchesInView = NO;
}
- (void)hideKeyBoard{
    [self.tableView endEditing:YES];
}
- (void)setupNav{
    UIBarButtonItem *rightItem= [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(save)];
//    rightItem.tintColor = [UIColor blackColor];
    self.navigationItem.rightBarButtonItem = rightItem;
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
}
- (void)save{
    NSString *sexStr;
    if (_selectFemale &&_selectMale) {
        sexStr = @"男、女";
    }else if(_selectMale&&!_selectFemale){
        sexStr = @"男";
    }else{
        sexStr = @"女";
    }
    
    NSString *colorStr;
    NSMutableArray *temArr = [NSMutableArray array];
    for (MycolorModel *model in _selectedColorArray) {
        NSString *str = model.colorName;
        [temArr addObject:str];
    }
    colorStr = [temArr componentsJoinedByString:@","];
  
    
    NSMutableString *sizeStr = [NSMutableString string];
    for (NSString *str in _selectSizeArr) {
        [sizeStr appendString:str];
    }
    NSString *finaStr = [NSString stringWithFormat:@"%@,%@,%@",@"",colorStr,sizeStr];
    
    NSUInteger index = self.navigationController.viewControllers.count-2;
    AddGoodsTableViewController *vc = self.navigationController.viewControllers[index];
    vc.standardsCell.detailTextLabel.text = finaStr;
    vc.selectMale = _selectMale;
    vc.selectFemale = _selectFemale;
    
    NSString *size;
    if (_selectSizeArr.count > 0) {
        size = [_selectSizeArr componentsJoinedByString:@","];
    }
    vc.size = size;
    vc.color = colorStr;
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)setUpTableView{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tabBarController.tabBar.hidden = YES;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.tableView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"headView"];
    _titleArr = @[@"请选择颜色",@"请选择尺寸",@"性别"];
}
- (void)unparsedColor{
    // 注意JSON后缀的东西和Plist不同，Plist可以直接通过contentOfFile抓取，Json要先打成字符串，然后用工具转换
    NSString *path = [[NSBundle mainBundle] pathForResource:@"colors" ofType:@"json"];
    NSLog(@"%@",path);
    NSString *jsonStr = [NSString stringWithContentsOfFile:path usedEncoding:nil error:nil];
    NSArray *arr = [jsonStr mj_JSONObject];
    _mycolorModelArr = [NSMutableArray array];
    for (NSDictionary *dic in arr) {
        MycolorModel *model = [[MycolorModel alloc] initMycolorModelWithDictionary:dic];
        [_mycolorModelArr addObject:model];
    }
}
- (void)requestSize{
    NSString *url = [kBaseURL stringByAppendingPathComponent:kQuery_goods_size];
    AFHTTPSessionManager *manager = [CommonUtils getAFHTTPSessionManager];
    [manager POST:url parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {//参数：typeNo(商品二级类型编号)
        
        if (_typeNo != nil) {
            [formData appendPartWithFormData:[_typeNo dataUsingEncoding:NSUTF8StringEncoding] name:@"typeNo"];
        }
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@",responseObject);
        NSString *result = responseObject[@"result"];
        //NSString *message = responseObject[@"message"];
        if ([result integerValue] == 1) {//成功
            _sizeArr = responseObject[@"data"];
            
        }
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0){
        return (_mycolorModelArr.count/3+1) *46;
    }else{
        return (_sizeArr.count/3+1)*50;
    }
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UITableViewHeaderFooterView *headView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"headView"];
    if (headView == nil) {
        headView = [[UITableViewHeaderFooterView alloc] init];
    }
    headView.textLabel.font = [UIFont systemFontOfSize:13];
    headView.textLabel.text = _titleArr[section];
    return headView;
    
}
-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section{
    UITableViewHeaderFooterView *headView = (UITableViewHeaderFooterView *)view;
    headView.backgroundColor = [UIColor cyanColor];
    headView.textLabel.font = [UIFont systemFontOfSize:11];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0){
        ColorTableViewCell *cell = [[NSBundle mainBundle] loadNibNamed:@"ColorTableViewCell" owner:nil options:nil][0];
        cell.mycolorModelArr = _mycolorModelArr;
        cell.delegate = self;
        return cell;
    }else{
        SizeTableViewCell *cell = [[NSBundle mainBundle] loadNibNamed:@"SizeTableViewCell" owner:nil options:nil][0];
        cell.delegate = self;
        cell.sizeArr = [NSMutableArray arrayWithArray:_sizeArr];
        return cell;
    }
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
}
#pragma mark - SexTableViewCellDelegate
-(void)didSelectSexButtonWithMale:(BOOL)selectMale female:(BOOL)selectFmale{
    _selectMale = selectMale;
    _selectFemale = selectFmale;
}
#pragma mark - ColorTableViewCellDelegate
-(void)didSelectColorsWithArray:(NSArray *)colorsArray{
    NSLog(@"选择的颜色：%@",colorsArray);
    _selectedColorArray = colorsArray;
    [Account sharedAccount].colorArr = colorsArray;
}
-(void)didAddColorWithColorName:(NSString *)name{
    NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:1];
    ColorTableViewCell *cell = (ColorTableViewCell *)[self.tableView cellForRowAtIndexPath:index];
    CGRect frame2 = cell.frame;
    frame2.size.height = ((_mycolorModelArr.count+1)/3+1) *46;
    cell.frame = frame2;
    [self.tableView beginUpdates];
    [self.tableView endUpdates];
}
#pragma mark - SizeTableViewCellDelegate
-(void)didSelectSizeWithArray:(NSArray *)selectSizeArray{
    NSLog(@"选择的size:%@",selectSizeArray);
    _selectSizeArr = selectSizeArray;
}
- (void)didAddSizeWithSize:(NSString *)size{
    NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:2];
    SizeTableViewCell *cell = (SizeTableViewCell *)[self.tableView cellForRowAtIndexPath:index];
    CGRect frame2 = cell.frame;
    frame2.size.height = ((_sizeArr.count+1)/3+1) *50;
    cell.frame = frame2;
    [self.tableView beginUpdates];
    [self.tableView endUpdates];
}



@end
